
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>用户态驱动 · book for nxos</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="nxos development team">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="ubpf.html" />
    
    
    <link rel="prev" href="userver.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://book-os.com" target="_blank" class="custom-link">bookos</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" >
            
                <span>
            
                    
                    NXOS文档中心
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" >
            
                <span>
            
                    
                    简介
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    NXOS 简介
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.2" >
            
                <span>
            
                    
                    快速上手
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.2.1" data-path="../../tutorial/quick-start.html">
            
                <a href="../../tutorial/quick-start.html">
            
                    
                    虚拟平台运行
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2.2" data-path="../../tutorial/build-romdisk.html">
            
                <a href="../../tutorial/build-romdisk.html">
            
                    
                    构建romdisk
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3" >
            
                <span>
            
                    
                    内核
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.1" data-path="../base/framework.html">
            
                <a href="../base/framework.html">
            
                    
                    整体框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.2" data-path="../base/start.html">
            
                <a href="../base/start.html">
            
                    
                    启动流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.3" data-path="../mm/mm.html">
            
                <a href="../mm/mm.html">
            
                    
                    内存管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.3.1" data-path="../mm/buddy.html">
            
                <a href="../mm/buddy.html">
            
                    
                    buddy伙伴算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.3.2" data-path="../mm/heap.html">
            
                <a href="../mm/heap.html">
            
                    
                    heapCache虚拟内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.3.3" data-path="../mm/vmspace.html">
            
                <a href="../mm/vmspace.html">
            
                    
                    vmspace虚拟地址空间
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3.4" data-path="../sched/sched.html">
            
                <a href="../sched/sched.html">
            
                    
                    调度管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.4.1" data-path="../sched/thread.html">
            
                <a href="../sched/thread.html">
            
                    
                    线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.4.2" data-path="../sched/smp.html">
            
                <a href="../sched/smp.html">
            
                    
                    SMP多核调度
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3.5" data-path="../process/process.html">
            
                <a href="../process/process.html">
            
                    
                    进程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.5.1" data-path="../process/syscall.html">
            
                <a href="../process/syscall.html">
            
                    
                    系统调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3.6" data-path="../interrupt/irq.html">
            
                <a href="../interrupt/irq.html">
            
                    
                    中断系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.6.1" data-path="../interrupt/delay_queue.html">
            
                <a href="../interrupt/delay_queue.html">
            
                    
                    延迟队列
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3.7" data-path="../timer/timer.html">
            
                <a href="../timer/timer.html">
            
                    
                    定时器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.4" >
            
                <span>
            
                    
                    内核移植
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.4.1" data-path="../port/riscv/riscv.html">
            
                <a href="../port/riscv/riscv.html">
            
                    
                    riscv架构移植
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.5" >
            
                <span>
            
                    
                    平台支持
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.5.1" data-path="../platform/i386.html">
            
                <a href="../platform/i386.html">
            
                    
                    i386
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5.2" data-path="../platform/qemu_riscv64.html">
            
                <a href="../platform/qemu_riscv64.html">
            
                    
                    qemu_riscv64
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5.3" data-path="../platform/k210.html">
            
                <a href="../platform/k210.html">
            
                    
                    k210
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5.4" data-path="../platform/d1.html">
            
                <a href="../platform/d1.html">
            
                    
                    d1-nezha
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5.5" data-path="../platform/hifive_unmached.html">
            
                <a href="../platform/hifive_unmached.html">
            
                    
                    hifive unmached
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.6" >
            
                <span>
            
                    
                    用户态服务和驱动框架
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.6.1" data-path="userver.html">
            
                <a href="userver.html">
            
                    
                    用户态服务
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.1.6.2" data-path="udriver.html">
            
                <a href="udriver.html">
            
                    
                    用户态驱动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6.3" data-path="ubpf.html">
            
                <a href="ubpf.html">
            
                    
                    UBPF
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.7" >
            
                <span>
            
                    
                    用户接口
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.7.1" data-path="api.html">
            
                <a href="api.html">
            
                    
                    API设计
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.8" >
            
                <span>
            
                    
                    编程开发
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.8.1" data-path="../../tutorial/code-style.html">
            
                <a href="../../tutorial/code-style.html">
            
                    
                    代码风格
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8.2" data-path="../../tutorial/commit-rule.html">
            
                <a href="../../tutorial/commit-rule.html">
            
                    
                    提交规则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8.3" data-path="../../tutorial/menuconfig.html">
            
                <a href="../../tutorial/menuconfig.html">
            
                    
                    菜单配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8.4" data-path="../../tutorial/toolchains.html">
            
                <a href="../../tutorial/toolchains.html">
            
                    
                    工具链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8.5" data-path="../../tutorial/utest.html">
            
                <a href="../../tutorial/utest.html">
            
                    
                    单元测试
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >用户态驱动</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7528;&#x6237;&#x6001;&#x9A71;&#x52A8;">&#x7528;&#x6237;&#x6001;&#x9A71;&#x52A8;</h1>
<h2 id="&#x63CF;&#x8FF0;">&#x63CF;&#x8FF0;</h2>
<p>&#x7528;&#x6237;&#x6001;&#x9A71;&#x52A8;&#x4F5C;&#x4E3A;&#x5185;&#x6838;&#x529F;&#x80FD;&#x7684;&#x62D3;&#x5C55;&#xFF0C;&#x4E00;&#x822C;&#x90FD;&#x662F;&#x5BF9;&#x7269;&#x7406;&#x8BBE;&#x5907;&#x7684;&#x63A5;&#x53E3;&#x5C01;&#x88C5;&#x548C;&#x62BD;&#x8C61;&#x3002;
&#x5728;&#x5185;&#x6838;&#x4E2D;&#x4F1A;&#x6709;&#x4E00;&#x4E9B;&#x57FA;&#x7840;&#x7684;&#x9A71;&#x52A8;&#xFF0C;&#x548C;&#x5185;&#x6838;&#x4E00;&#x8D77;&#x7F16;&#x8BD1;&#x3002;
&#x4F46;&#x662F;&#xFF0C;&#x5728;&#x6709;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x9A71;&#x52A8;&#x7A0B;&#x5E8F;&#x7531;&#x4E8E;&#x786C;&#x4EF6;&#x72B6;&#x6001;&#x7684;&#x6539;&#x53D8;&#xFF0C;&#x5BFC;&#x81F4;&#x7A0B;&#x5E8F;&#x5D29;&#x6E83;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4F1A;&#x5F71;&#x54CD;&#x5185;&#x6838;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x628A;&#x9A71;&#x52A8;&#x653E;&#x5230;&#x7528;&#x6237;&#x6001;&#xFF0C;
&#x53EF;&#x4EE5;&#x907F;&#x514D;&#x7531;&#x4E8E;&#x9A71;&#x52A8;&#x7A0B;&#x5E8F;&#x7684;&#x5D29;&#x6E83;&#x5BFC;&#x81F4;&#x5185;&#x6838;&#x6545;&#x969C;&#x3002;</p>
<p>&#x5185;&#x6838;&#x9700;&#x8981;&#x505A;&#x7684;&#x5C31;&#x662F;&#x5BF9;&#x8BBE;&#x5907;&#x8D44;&#x6E90;&#x7684;&#x62BD;&#x8C61;&#xFF0C;&#x5E76;&#x66B4;&#x9732;&#x4E00;&#x4E9B;&#x63A5;&#x53E3;&#x7ED9;&#x7528;&#x6237;&#x6001;&#x9A71;&#x52A8;&#x53BB;&#x4F7F;&#x7528;&#xFF0C;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x8FD9;&#x4E9B;&#x63A5;&#x53E3;&#x6765;&#x5F00;&#x53D1;&#x9A71;&#x52A8;&#x3002;</p>
<p>&#x5176;&#x4E2D;&#xFF0C;&#x6700;&#x4E3A;&#x6838;&#x5FC3;&#x7684;&#x529F;&#x80FD;&#x5C31;&#x662F;&#x5BF9;&#x4E2D;&#x65AD;&#x7684;&#x5904;&#x7406;&#xFF0C;&#x5982;&#x4F55;&#x53CA;&#x65F6;&#x54CD;&#x5E94;&#x4E2D;&#x65AD;&#xFF0C;&#x5E76;&#x5BF9;&#x4E2D;&#x65AD;&#x8FDB;&#x884C;&#x5E94;&#x7B54;&#xFF0C;&#x662F;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x53BB;&#x7814;&#x7A76;&#x7684;&#x91CD;&#x8981;&#x5185;&#x5BB9;&#x3002;</p>
<p><code>NXOS</code> &#x91C7;&#x53D6;&#x7684;&#x7B56;&#x7565;&#x662F;&#x5185;&#x6838;&#x548C;&#x7528;&#x6237;&#x6001;&#x76F8;&#x7ED3;&#x5408;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x6240;&#x6709;&#x7684;&#x64CD;&#x4F5C;&#x90FD;&#x662F;&#x901A;&#x8FC7;&#x7528;&#x6237;&#x81EA;&#x5DF1;&#x53BB;&#x5B8C;&#x6210;&#x3002;
&#x56E0;&#x6B64;&#xFF0C;&#x5185;&#x6838;&#x9700;&#x8981;&#x5BF9;&#x67D0;&#x4E2A;&#x9A71;&#x52A8;&#x7684;&#x5F00;&#x53D1;&#x505A;&#x4E00;&#x5B9A;&#x5E95;&#x5C42;&#x7684;&#x62BD;&#x8C61;&#x4E0E;&#x5C01;&#x88C5;&#xFF0C;&#x5E76;&#x66B4;&#x9732;&#x63A5;&#x53E3;&#x7ED9;&#x7528;&#x6237;&#x9A71;&#x52A8;&#x4F7F;&#x7528;&#x3002;</p>
<p><img src="figures/udriver.png" alt="&#x7528;&#x6237;&#x6001;&#x9A71;&#x52A8;&#x6846;&#x67B6;"></p>
<h2 id="&#x529F;&#x80FD;&#x652F;&#x6301;">&#x529F;&#x80FD;&#x652F;&#x6301;</h2>
<p>&#x5728;&#x7528;&#x6237;&#x6001;&#x4E2D;&#x4E5F;&#x652F;&#x6301;&#x4E86;NX_Device&#x548C;NX_Driver&#x7B49;&#x7ED3;&#x6784;&#xFF0C;&#x53EF;&#x4EE5;&#x8DDF;&#x5728;&#x5185;&#x6838;&#x4E2D;&#x4E00;&#x6837;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x5F53;&#x6267;&#x884C;NX_DriverRegister&#x7684;&#x65F6;&#x5019;&#x5C31;&#x4F1A;&#x6CE8;&#x518C;&#x6539;&#x9A71;&#x52A8;&#x4FE1;&#x606F;&#x5230;&#x5185;&#x6838;&#x7ED3;&#x6784;&#x4F53;&#x4E2D;&#xFF0C;&#x4ECE;&#x6B64;&#x4FBF;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x901A;&#x7528;&#x7684;&#x8BBE;&#x5907;&#x63A5;&#x53E3;&#x8BBF;&#x95EE;&#x6539;&#x8BBE;&#x5907;&#x4E86;&#x3002;</p>
<p>&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#xFF0C;&#x4F9D;&#x8D56;&#x4E8E;&#x5185;&#x6838;&#x7684;udrver&#x9A71;&#x52A8;&#x6846;&#x67B6;&#x6765;&#x5B9E;&#x73B0;&#x3002;&#x901A;&#x8FC7; <code>hub</code> &#x673A;&#x5236;&#x8F6C;&#x53D1;&#x64CD;&#x4F5C;&#x8BF7;&#x6C42;&#x5230;&#x7528;&#x6237;&#x6001;&#x9A71;&#x52A8;&#xFF0C;&#x9A71;&#x52A8;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x540E;&#x8FD4;&#x56DE;&#xFF0C;&#x5373;&#x53EF;&#x5B8C;&#x6210;&#x6539;&#x64CD;&#x4F5C;&#x3002;</p>
<ul>
<li>&#x6587;&#x4EF6;&#xFF1A;src/drivers/udriver/udriver.c</li>
</ul>
<pre><code class="lang-c"><span class="hljs-comment">/**
 * Copyright (c) 2018-2022, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Contains: udriver driver
 * 
 * Change Logs:
 * Date           Author            Notes
 * 2022-3-15      JasonHu           Init
 */</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;base/driver.h&gt;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> NX_LOG_NAME <span class="hljs-string">&quot;udriver&quot;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;base/log.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;base/hub.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;base/string.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;base/malloc.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;base/debug.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;base/uaccess.h&gt;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> UDRIVER_HUB_PREFIX <span class="hljs-string">&quot;udrv_&quot;</span></span>

<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> DriverExtension
{
    NX_UserDriver * udrv; <span class="hljs-comment">/* user driver */</span>
    <span class="hljs-keyword">char</span> hubName[NX_HUB_NAME_LEN];   <span class="hljs-comment">/* udriver hub name */</span>
} DriverExtension;

<span class="hljs-keyword">enum</span> UdriverOpsAPI
{
    UDRIVER_OPS_OPEN = <span class="hljs-number">0</span>,
    UDRIVER_OPS_CLOSE,
    UDRIVER_OPS_READ,
    UDRIVER_OPS_WRITE,
    UDRIVER_OPS_CONTROL,
    UDRIVER_OPS_MAPPABLE,
};

<span class="hljs-function">NX_PRIVATE NX_Error <span class="hljs-title">UdriverOpen</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> NX_Device *device, NX_U32 flags)</span>
</span>{
    NX_Error err;
    NX_Driver * kdrv;
    NX_UserDevice * udev;
    NX_Size retVal = <span class="hljs-number">0</span>;
    NX_HubParam param;
    DriverExtension * drvext;

    kdrv = device-&gt;driver;
    udev = (NX_UserDevice * )device-&gt;extension;
    drvext = (DriverExtension * )kdrv-&gt;extension;

    param.api = UDRIVER_OPS_OPEN;
    param.args[<span class="hljs-number">0</span>] = (NX_Size)drvext-&gt;udrv;
    param.args[<span class="hljs-number">1</span>] = (NX_Size)udev;
    param.args[<span class="hljs-number">2</span>] = (NX_Size)flags;

    <span class="hljs-keyword">if</span> ((err = NX_HubCallParamName(drvext-&gt;hubName, &amp;param, &amp;retVal)) != NX_EOK)
    {
        <span class="hljs-keyword">return</span> err;
    }

    <span class="hljs-keyword">return</span> retVal;
}

<span class="hljs-function">NX_PRIVATE NX_Error <span class="hljs-title">UdriverClose</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> NX_Device *device)</span>
</span>{
    NX_Error err;
    NX_Driver * kdrv;
    NX_UserDevice * udev;
    NX_Size retVal = <span class="hljs-number">0</span>;
    NX_HubParam param;
    DriverExtension * drvext;

    kdrv = device-&gt;driver;
    udev = (NX_UserDevice * )device-&gt;extension;
    drvext = (DriverExtension * )kdrv-&gt;extension;

    param.api = UDRIVER_OPS_CLOSE;
    param.args[<span class="hljs-number">0</span>] = (NX_Size)drvext-&gt;udrv;
    param.args[<span class="hljs-number">1</span>] = (NX_Size)udev;

    <span class="hljs-keyword">if</span> ((err = NX_HubCallParamName(drvext-&gt;hubName, &amp;param, &amp;retVal)) != NX_EOK)
    {
        <span class="hljs-keyword">return</span> err;
    }

    <span class="hljs-keyword">return</span> retVal;
}

<span class="hljs-function">NX_PRIVATE NX_Error <span class="hljs-title">UdriverRead</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> NX_Device *device, <span class="hljs-keyword">void</span> *buf, NX_Offset off, NX_Size len, NX_Size *outLen)</span>
</span>{
    NX_Error err;
    NX_Driver * kdrv;
    NX_UserDevice * udev;
    NX_Size retVal = <span class="hljs-number">0</span>;
    NX_HubParam param;
    DriverExtension * drvext;

    kdrv = device-&gt;driver;
    udev = (NX_UserDevice * )device-&gt;extension;
    drvext = (DriverExtension * )kdrv-&gt;extension;

    param.api = UDRIVER_OPS_READ;
    param.args[<span class="hljs-number">0</span>] = (NX_Size)drvext-&gt;udrv;
    param.args[<span class="hljs-number">1</span>] = (NX_Size)udev;
    param.args[<span class="hljs-number">2</span>] = (NX_Size)buf;
    param.args[<span class="hljs-number">3</span>] = (NX_Size)off;
    param.args[<span class="hljs-number">4</span>] = (NX_Size)len;
    param.args[<span class="hljs-number">5</span>] = (NX_Size)outLen;

    <span class="hljs-keyword">if</span> ((err = NX_HubCallParamName(drvext-&gt;hubName, &amp;param, &amp;retVal)) != NX_EOK)
    {
        <span class="hljs-keyword">return</span> err;
    }
    <span class="hljs-keyword">return</span> retVal;
}

<span class="hljs-function">NX_PRIVATE NX_Error <span class="hljs-title">UdriverWrite</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> NX_Device *device, <span class="hljs-keyword">void</span> *buf, NX_Offset off, NX_Size len, NX_Size *outLen)</span>
</span>{
    NX_Error err;
    NX_Driver * kdrv;
    NX_UserDevice * udev;
    NX_Size retVal = <span class="hljs-number">0</span>;
    NX_HubParam param;
    DriverExtension * drvext;

    kdrv = device-&gt;driver;
    udev = (NX_UserDevice * )device-&gt;extension;
    drvext = (DriverExtension * )kdrv-&gt;extension;

    param.api = UDRIVER_OPS_WRITE;
    param.args[<span class="hljs-number">0</span>] = (NX_Size)drvext-&gt;udrv;
    param.args[<span class="hljs-number">1</span>] = (NX_Size)udev;
    param.args[<span class="hljs-number">2</span>] = (NX_Size)buf;
    param.args[<span class="hljs-number">3</span>] = (NX_Size)off;
    param.args[<span class="hljs-number">4</span>] = (NX_Size)len;
    param.args[<span class="hljs-number">5</span>] = (NX_Size)outLen;

    <span class="hljs-keyword">if</span> ((err = NX_HubCallParamName(drvext-&gt;hubName, &amp;param, &amp;retVal)) != NX_EOK)
    {
        <span class="hljs-keyword">return</span> err;
    }
    <span class="hljs-keyword">return</span> retVal;
}

<span class="hljs-function">NX_PRIVATE NX_Error <span class="hljs-title">UdriverControl</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> NX_Device *device, NX_U32 cmd, <span class="hljs-keyword">void</span> *arg)</span>
</span>{
    NX_Error err;
    NX_Driver * kdrv;
    NX_UserDevice * udev;
    NX_Size retVal = <span class="hljs-number">0</span>;
    NX_HubParam param;
    DriverExtension * drvext;

    kdrv = device-&gt;driver;
    udev = (NX_UserDevice * )device-&gt;extension;
    drvext = (DriverExtension * )kdrv-&gt;extension;

    param.api = UDRIVER_OPS_CONTROL;
    param.args[<span class="hljs-number">0</span>] = (NX_Size)drvext-&gt;udrv;
    param.args[<span class="hljs-number">1</span>] = (NX_Size)udev;
    param.args[<span class="hljs-number">2</span>] = (NX_Size)cmd;
    param.args[<span class="hljs-number">3</span>] = (NX_Size)arg;

    <span class="hljs-keyword">if</span> ((err = NX_HubCallParamName(drvext-&gt;hubName, &amp;param, &amp;retVal)) != NX_EOK)
    {
        <span class="hljs-keyword">return</span> err;
    }

    <span class="hljs-keyword">return</span> retVal;
}

<span class="hljs-function">NX_PRIVATE NX_Error <span class="hljs-title">UdriverMappable</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> NX_Device *device, NX_Size length, NX_U32 prot, NX_Addr * outPhyAddr)</span>
</span>{
    NX_Error err;
    NX_Driver * kdrv;
    NX_UserDevice * udev;
    NX_Size retVal = <span class="hljs-number">0</span>;
    NX_HubParam param;
    DriverExtension * drvext;

    kdrv = device-&gt;driver;
    udev = (NX_UserDevice * )device-&gt;extension;
    drvext = (DriverExtension * )kdrv-&gt;extension;

    param.api = UDRIVER_OPS_MAPPABLE;
    param.args[<span class="hljs-number">0</span>] = (NX_Size)drvext-&gt;udrv;
    param.args[<span class="hljs-number">1</span>] = (NX_Size)udev;
    param.args[<span class="hljs-number">2</span>] = (NX_Size)length;
    param.args[<span class="hljs-number">3</span>] = (NX_Size)prot;
    param.args[<span class="hljs-number">4</span>] = (NX_Size)outPhyAddr;

    <span class="hljs-keyword">if</span> ((err = NX_HubCallParamName(drvext-&gt;hubName, &amp;param, &amp;retVal)) != NX_EOK)
    {
        <span class="hljs-keyword">return</span> err;
    }

    <span class="hljs-keyword">return</span> retVal;
}

NX_PRIVATE NX_DriverOps UdriverDriverOps = {
    .open = UdriverOpen,
    .close = UdriverClose,
    .read = UdriverRead,
    .write = UdriverWrite,
    .control = UdriverControl,
    .mappable = UdriverMappable,
};

<span class="hljs-function">NX_PRIVATE NX_Error <span class="hljs-title">UdriverUnregister</span><span class="hljs-params">(NX_Driver * drv)</span>
</span>{
    DriverExtension * drvext;
    NX_Error err;

    <span class="hljs-keyword">if</span> (!drv)
    {
        <span class="hljs-keyword">return</span> NX_EINVAL;
    }

    drvext = (DriverExtension * )drv-&gt;extension;

    <span class="hljs-keyword">if</span> ((err = NX_HubUnregister(drvext-&gt;hubName)) != NX_EOK)
    {
        NX_LOG_W(<span class="hljs-string">&quot;udriver %s unregister hub %s failed with error %s!&quot;</span>, drv-&gt;name, drvext-&gt;hubName, NX_ErrorToString(err));
        <span class="hljs-keyword">return</span> err;
    }

    NX_Device *device, *n;
    NX_ListForEachEntrySafe(device, n, &amp;drv-&gt;deviceListHead, <span class="hljs-built_in">list</span>)
    {
        NX_DriverDetachDevice(drv, device-&gt;name);
    }
    <span class="hljs-keyword">if</span> (drv-&gt;extension)
    {
        NX_MemFree(drv-&gt;extension);
    }
    NX_DriverUnregister(drv);
    NX_DriverDestroy(drv);
    <span class="hljs-keyword">return</span> NX_EOK;
}

<span class="hljs-function">NX_PRIVATE NX_Error <span class="hljs-title">UdriverCloseSolt</span><span class="hljs-params">(<span class="hljs-keyword">void</span> * object, NX_ExposedObjectType type)</span>
</span>{
    NX_Driver * drv;

    <span class="hljs-keyword">if</span> (type != NX_EXOBJ_UDRIVER)
    {
        <span class="hljs-keyword">return</span> NX_ENORES;
    }

    drv = (NX_Driver *) object;
    NX_ASSERT(drv);

    <span class="hljs-keyword">return</span> UdriverUnregister(drv);
}

<span class="hljs-function">NX_Error <span class="hljs-title">NX_UserDriverRegister</span><span class="hljs-params">(NX_UserDriver * drv, NX_Solt * outSolt)</span>
</span>{
    <span class="hljs-keyword">if</span> (!drv)
    {
        <span class="hljs-keyword">return</span> NX_EINVAL;
    }

    NX_Solt solt = NX_SOLT_INVALID_VALUE;
    NX_Error err;
    DriverExtension * drvext;
    NX_Device *device;
    <span class="hljs-keyword">char</span> hubName[NX_HUB_NAME_LEN + <span class="hljs-number">1</span>] = {<span class="hljs-number">0</span>};
    NX_Driver *driver = NX_DriverCreate(drv-&gt;name, drv-&gt;type, drv-&gt;flags, &amp;UdriverDriverOps);
    <span class="hljs-keyword">if</span> (driver == NX_NULL)
    {
        NX_LOG_E(<span class="hljs-string">&quot;create udriver %s failed!&quot;</span>, drv-&gt;name);
        <span class="hljs-keyword">return</span> NX_ENOMEM;
    }

    drvext = NX_MemAlloc(<span class="hljs-keyword">sizeof</span>(DriverExtension));
    <span class="hljs-keyword">if</span> (drvext == NX_NULL)
    {
        NX_LOG_E(<span class="hljs-string">&quot;alloc udriver %s extension failed!&quot;</span>, drv-&gt;name);
        NX_DriverDestroy(driver);
        <span class="hljs-keyword">return</span> NX_ENOMEM;
    }

    drvext-&gt;udrv = drv;
    NX_StrCopy(hubName, UDRIVER_HUB_PREFIX);
    NX_StrCat(hubName, drv-&gt;name);
    NX_StrCopy(drvext-&gt;hubName, hubName);

    driver-&gt;extension = drvext;

    <span class="hljs-comment">/* register hub server */</span>
    <span class="hljs-keyword">if</span> ((err = NX_HubRegister(hubName, <span class="hljs-number">1</span>, NX_NULL)) != NX_EOK) <span class="hljs-comment">/* driver only opened onece */</span>
    {
        NX_LOG_E(<span class="hljs-string">&quot;udriver %s register hub %s failed with err %s!&quot;</span>, drv-&gt;name, hubName, NX_ErrorToString(err));
        NX_MemFree(drvext);
        NX_DriverDestroy(driver);
        <span class="hljs-keyword">return</span> err;
    }

    NX_UserDevice * userDev;

    NX_ListForEachEntry (userDev, &amp;drv-&gt;deviceListHead, <span class="hljs-built_in">list</span>)
    {
        <span class="hljs-keyword">if</span> ((err = NX_DriverAttachDevice(driver, userDev-&gt;name, &amp;device)) != NX_EOK)
        {
            NX_LOG_E(<span class="hljs-string">&quot;udriver %s attach device %s failed with err %s!&quot;</span>, drv-&gt;name, userDev-&gt;name, NX_ErrorToString(err));
            NX_HubUnregister(hubName);
            NX_MemFree(drvext);
            NX_DriverCleanup(drv-&gt;name);
            <span class="hljs-keyword">return</span> NX_ENOMEM;
        }
        device-&gt;extension = userDev;
    }

    <span class="hljs-keyword">if</span> ((err = NX_DriverRegister(driver)) != NX_EOK)
    {
        NX_LOG_E(<span class="hljs-string">&quot;udriver %s register failed with err %s!&quot;</span>, drv-&gt;name, NX_ErrorToString(err));
        NX_HubUnregister(hubName);
        NX_MemFree(drvext);
        NX_DriverCleanup(drv-&gt;name);
        <span class="hljs-keyword">return</span> err;
    }

    <span class="hljs-comment">/* install udriver exobj */</span>
    <span class="hljs-keyword">if</span> (NX_ProcessInstallSolt(NX_ProcessCurrent(), driver, NX_EXOBJ_UDRIVER, UdriverCloseSolt, &amp;solt) != NX_EOK)
    {
        NX_LOG_E(<span class="hljs-string">&quot;udriver %s install failed with err %s!&quot;</span>, drv-&gt;name, NX_ErrorToString(err));
        NX_HubUnregister(hubName);
        NX_MemFree(drvext);
        NX_DriverCleanup(drv-&gt;name);
        <span class="hljs-keyword">return</span> err;
    }

    <span class="hljs-keyword">if</span> (outSolt)
    {
        NX_CopyToUser((<span class="hljs-keyword">char</span> *)outSolt, (<span class="hljs-keyword">char</span> *)&amp;solt, <span class="hljs-keyword">sizeof</span>(solt));
    }

    <span class="hljs-keyword">return</span> err;
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="userver.html" class="navigation navigation-prev " aria-label="Previous page: 用户态服务">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="ubpf.html" class="navigation navigation-next " aria-label="Next page: UBPF">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"用户态驱动","level":"1.1.6.2","depth":3,"next":{"title":"UBPF","level":"1.1.6.3","depth":3,"path":"programing-manual/user/ubpf.md","ref":"programing-manual/user/ubpf.md","articles":[]},"previous":{"title":"用户态服务","level":"1.1.6.1","depth":3,"path":"programing-manual/user/userver.md","ref":"programing-manual/user/userver.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy BookOS-developers 2022","modify_label":"修订时间：","modify_format":"YYYY-MM-DD"},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"nxos development team","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"book for nxos","language":"zh-hans","links":{"sidebar":{"bookos":"http://book-os.com"}},"gitbook":"3.2.3","description":"document for nxos kernel"},"file":{"path":"programing-manual/user/udriver.md","mtime":"2022-06-21T13:54:32.025Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-06-21T14:02:23.265Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

